require(["../path/path.js","../js/main.js"], () => {
	require(["jquery","template"],($,template)=>{
		
		let user_id="";
		
		//拿到cookie商品数据
		let cookieStr=document.cookie.split("; ");
		let data="";
		for(let key of cookieStr){
			if(/shop=/.test(key)){
				data=JSON.parse(key.substring(5));
			}
			if(/user_id=/.test(key)){
				user_id=JSON.parse(key.substring(8)).id;
			}
		}
		
		
		
		let hcTemp="";//作为缓存template
		//请求模板 渲染数据
		$.ajax({
			url:"/shopping_car_tr",
			success: function(temp){
				let html=template.render(temp,{data})
				// console.log(temp)
				// console.log(data)
				console.log(data);
				if(data[0]==undefined){
					$("#shopping_car").hide()
					$("#empty_car").show()
				}else {
					hcTemp=temp;
					$(".shop_tab").html(html);
					
					fn();
				}
			}
		})
		
		function fn(){//渲染后从新加上点击事件
		
			$("._oper button").bind("click",function(){
				let product_id=$(this).attr("product_id")
				delete_data(product_id);
			})//删除数据
			
			$(".red").bind("click",function(){
				let count=$(this).next().text();
				if(count>1){
					count--;
					$(this).next().text(count)
					
					amend_red($(this).attr("product_id"));
				}else if(count==1){
					$(this).next().text(count)
					if(confirm("你确定要删除该商品吗")){
						let product_id=$(this).attr("product_id")
						delete_data(product_id);
					}
				}
			})
			
			$(".add").bind("click",function(){
				let count=$(this).prev().text();
				if(count<50){
					count++;
					$(this).prev().text(count)
					amend_add($(this).attr("product_id"));
				}
			})
			
			
			
			
			function delete_data(product_id){
				
				let post_data=`[{"user_id":${user_id},"id":${product_id}}]`
				$.ajax({//请求服务器删除数据
					type:"post",
					url:"/delete",
					data: "filename=user_shopList.json&data="+post_data,
					success:function(data){
						console.log(data);
						if(data[0]==undefined){
							let d=new Date();
							d.setDate(d.getDate()-10);
							console.log(d);
							document.cookie="shop=null; expires="+d+";path=/";
							$("#shopping_car").hide()
							$("#empty_car").show()
						}else {
							document.cookie="shop="+JSON.stringify(data[0])+";path=/";
							let html=template.render(hcTemp,{data:data[0]});
							console.log(html);
							$(".shop_tab").html(html);
							fn();
							
						}
						
					}
					
				})
			}
			
			
			function amend_red(id){
				for(let key of data){
					if(key.id==id){
						key.count--;
						document.cookie="shop="+JSON.stringify(data)+";path=/";
						settime(JSON.stringify(data));
						
						let html=template.render(hcTemp,{data});
						console.log(html);
						$(".shop_tab").html(html);
						fn();
					}
				}
			}
			function amend_add(id){
				for(let key of data){
					if(key.id==id){
						key.count++;
						document.cookie="shop="+JSON.stringify(data)+";path=/";
						settime(JSON.stringify(data));
						
						let html=template.render(hcTemp,{data});
						console.log(html);
						$(".shop_tab").html(html);
						fn();
					}
				}
			}
			
		}
		
		
		let settime=(function(){
			let timer=null;
			return function(data){
				clearTimeout(timer);
				timer=setTimeout(function(){
					$.ajax({	//发送到服务器存储
					   type: "POST",
					   url: "/change",
					   data: "filename=user_shopList.json&data="+data
					});
				},500)
			}
		})()
		
		
		
	})
})